package com.sky.controller.admin;

import com.sky.result.Result;
import com.sky.utils.MyAliUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.UUID;

@RestController
@RequestMapping("/admin/common")
@Slf4j
public class CommonController {
    @Autowired
    private MyAliUtils myAliUtils;

    @PostMapping("/upload")
    public Result<String> upLoadFile(MultipartFile file) throws IOException {
        // 获取文件后缀
        // 获取原始文件名
        String originalFilename = file.getOriginalFilename();
        // 检查文件名是否为空或没有后缀
        if (originalFilename == null || !originalFilename.contains(".")) {
            return Result.error("文件名不合法");
        }
        // 获取文件后缀
        String substring = originalFilename.substring(originalFilename.lastIndexOf("."));

        // 构造新的文件名
        String newFilename = UUID.randomUUID().toString() + substring;

        String img  = myAliUtils.upload(file.getBytes(),newFilename);
        return Result.success(img);
    }
}
